#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->tableWidget->setColumnCount(8);
    QStringList header;
    header<<"type"<<"name"<<"description"<<"categories"<<"adresses"<<"phone numbers"<<"email adresses"<<"sites";
    ui->tableWidget->setHorizontalHeaderLabels(header);
    connect(ui->tableWidget,SIGNAL(cellClicked(int,int)),this,SLOT(onCellClicked(int,int)));
    connect(ui->actionOpen_file,SIGNAL(triggered()),this,SLOT(onOpenFile()));
    connect(ui->actionSave_file,SIGNAL(triggered()),this,SLOT(onSaveFile()));
    connect(ui->actionClose_file,SIGNAL(triggered()),this,SLOT(onCloseFile()));
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::setImage(QByteArray image)
{
    ui->drawingWidget->load(image);
}

void MainWindow::appendTableRow(QStringList row)
{
    ui->tableWidget->setRowCount(ui->tableWidget->rowCount()+1);
    for(uint i=0;i<row.size() && i<ui->tableWidget->columnCount();i++)
    {
        QTableWidgetItem * item = new QTableWidgetItem(row[i]);
        ui->tableWidget->setItem(ui->tableWidget->rowCount()-1,i,item);
    }
}

void MainWindow::onCellClicked(int row,int column)
{
    emit shapeSelected(row);
}

void MainWindow::receiveShapes(QVector<QStringList> shapes)
{
    for(uint i=0; i<shapes.size(); i++)
    {
        appendTableRow(shapes.at(i));
    }
}

void MainWindow::redraw(QByteArray newData)
{
    setImage(newData);
    ui->drawingWidget->repaint();
}

void MainWindow::onOpenFile()
{
    QFileDialog dialog;
    dialog.setFileMode(QFileDialog::AnyFile);
    dialog.setDirectory(QDir::homePath());
    dialog.setFilter("SVG (*.svg);; all files (*.*)");

    QString fileToBeOpened;
    if(dialog.exec())
    {
        fileToBeOpened = dialog.selectedFiles().at(0);
    }
    if(!fileToBeOpened.isEmpty())
    {
        onCloseFile();
        emit openFile(fileToBeOpened);
    }
}

QVector<QStringList> MainWindow::getContentsOfTableView()
{
    QVector<QStringList> cells;
    for(uint i=0; i<ui->tableWidget->rowCount(); i++)
    {
        QStringList row;
        for(uint j=0; j<ui->tableWidget->columnCount();j++)
        {
            if(ui->tableWidget->item(i,j)!=0x0)
            {
                row.append(ui->tableWidget->item(i,j)->text());
            }
        }
//        qDebug()<<row.size()<<row;
        cells.append(row);
    }
    return cells;
}

void MainWindow::onSaveFile()
{
    QFileDialog dialog;
    dialog.setFileMode(QFileDialog::AnyFile);
    dialog.setDirectory(QDir::homePath());
    dialog.setFilter("SVG (*.svg);; all files (*.*)");
    QString fileToBeSaved;
    if(dialog.exec())
    {
        fileToBeSaved = dialog.selectedFiles().at(0);
    }
    if(!fileToBeSaved.isEmpty())
    {
        emit saveFile(fileToBeSaved,getContentsOfTableView());
    }
}


void MainWindow::onCloseFile()
{
    redraw(QByteArray(""));
    ui->tableWidget->clearContents();
    ui->tableWidget->setRowCount(0);
    emit closeFile();
}
